package com.Iconcat.system.controller;


import com.Iconcat.system.entity.CatAdmin;
import com.Iconcat.system.entity.CatMenu;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.CatMenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzq
 * @since 2023-11-19
 */
@RestController
@CrossOrigin
@Api(value = "系统管理菜单模块",tags = "系统管理菜单接口")
@RequestMapping("/system/cat-menu")
public class CatMenuController {
    @Resource
    private CatMenuService menuService;

    @GetMapping
    public Result getAllMenus() {
        List<CatMenu> list =menuService.list();
        return Result.ok().data("menu",list);
    }
    @GetMapping("/{menuId}")
    public Result getMenuById(@PathVariable Long menuId) {
        CatMenu id =menuService.getById(menuId);
        return Result.ok().data("menu",id);
    }

    @PostMapping("/add")
    public Result createMenu(@RequestBody CatMenu menu) {
        boolean saved = menuService.save(menu);
        if (saved) {
            return Result.ok().data("menu", menu);
        } else {
            return Result.error().message("菜单创建失败");
        }
    }

    @PostMapping("/update/{menuId}")
    public Result updateMenu(@PathVariable Integer menuId, @RequestBody CatMenu menu) {
        menu.setMenuId(menuId);
        boolean updated = menuService.updateById(menu);
        if (updated) {
            return Result.ok().data("menu", menu);
        } else {
            return Result.error().message("菜单更新失败");
        }
    }

    @PostMapping("/delete/{menuId}")
    public Result deleteMenuById(@PathVariable Long menuId) {
        boolean deleted = menuService.removeById(menuId);
        if (deleted) {
            return Result.ok().message("菜单删除成功");
        } else {
            return Result.error().message("菜单删除失败");
        }
    }
}

